import React from "react"
import { useForm, FormProvider } from 'react-hook-form'

/**
 * 
 * @param {*} formValidations yup验证的配置对象
 * @param {*} children 组件的子组件
 * @param {*} initialValues 默认form值
 * @param {*} onSubmit form提交事件
 * @returns 
 */
const Form = ({ children, initialValues = {}, formValidations, onSubmit }) => {
  const formDefault = {
    mode: 'onBlur',        
    defaultValues: initialValues,                
    resolver: formValidations.resolver
  }
  // 使用表单验证插件
  const methods = useForm(formDefault)
  return (
    <FormProvider {...methods}>
      <form
        autoComplete="off"
        onSubmit={methods.handleSubmit(onSubmit)}
      >{children}</form>
    </FormProvider>
  )
}

export default Form